/* Copyright (c) 2009 Matthias Kaeppler
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 * 
 * 
 * 
 * Modified for use in Mealssenger by Theodore Felix Leo
 */

package com.tfleo.mealssenger.adapter;

import java.util.List;

import com.github.ignition.core.widgets.RemoteImageView;
import com.tfleo.mealssenger.FoodActivity;
import com.tfleo.mealssenger.PlaceActivity;
import com.tfleo.mealssenger.R;
import com.tfleo.mealssenger.object.Food;
import com.tfleo.mealssenger.utility.IntentExtras;

import android.content.Context;
import android.content.Intent;
import android.graphics.Color;
import android.graphics.drawable.Drawable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Gallery;
import android.widget.TextView;


/**
 * Can be used as an adapter for an Android {@link Gallery} view. This adapter loads the images to
 * be shown from the web.
 * 
 * @author Matthias Kaeppler
 */
public class WebGalleryAdapter extends BaseAdapter {

    public static final int NO_DRAWABLE = -1;

    private List<Food> mObjects;

    private LayoutInflater mInflater;
    private Context mContext;

    private Drawable progressDrawable;

    public WebGalleryAdapter(Context context) {
        initialize(context, null, null, null);
    }

    /**
     * @param context
     *            the current context
     * @param imageUrls
     *            the set of image URLs which are to be loaded and displayed
     */
    public WebGalleryAdapter(Context context, List<Food> objects, LayoutInflater inflater) {
        mInflater = inflater;
    	initialize(context, objects, null, null);
    }

    /**
     * @param context
     *            the current context
     * @param imageUrls
     *            the set of image URLs which are to be loaded and displayed
     * @param progressDrawableResId
     *            the resource ID of the drawable that will be used for rendering progress
     */
    public WebGalleryAdapter(Context context, List<Food> objects, int progressDrawableResId) {
        initialize(context, objects, context.getResources().getDrawable(progressDrawableResId),
                null);
    }

    /**
     * @param context
     *            the current context
     * @param imageUrls
     *            the set of image URLs which are to be loaded and displayed
     * @param progressDrawableResId
     *            the resource ID of the drawable that will be used for rendering progress
     * @param errorDrawableId
     *            the resource ID of the drawable that will be used if a download error occurs
     */
    public WebGalleryAdapter(Context context, List<Food> objects, int progressDrawableResId,
            int errorDrawableId) {
        initialize(context, objects, progressDrawableResId == NO_DRAWABLE ? null : context
                .getResources().getDrawable(progressDrawableResId),
                errorDrawableId == NO_DRAWABLE ? null : context.getResources().getDrawable(
                        errorDrawableId));
    }

    private void initialize(Context context, List<Food> objects, Drawable progressDrawable,
            Drawable errorDrawable) {
        this.mObjects = objects;
        this.progressDrawable = progressDrawable;
        this.mContext = context;
    }

    @Override
    public int getCount() {
        return mObjects.size();
    }

    @Override
    public Object getItem(int position) {
        return mObjects.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    public void setObjects(List<Food> objects) {
        this.mObjects = objects;
    }

    public List<Food> getObjects() {
        return mObjects;
    }

    public void setProgressDrawable(Drawable progressDrawable) {
        this.progressDrawable = progressDrawable;
    }

    public Drawable getProgressDrawable() {
        return progressDrawable;
    }

    // TODO: both convertView and ViewHolder are pointless at the moment, since there's a framework
    // bug which causes views to not be cached in a Gallery widget:
    // http://code.google.com/p/android/issues/detail?id=3376
    
    /* Modified by Theodore Felix Leo for use within Mealssenger */
    @Override
    public View getView(int position, View convertView, ViewGroup parent) {

        final Food food = (Food) getItem(position);

        ViewHolder holder = null;
        RemoteImageView webImageView = null;
        TextView name = null;
        final TextView location;

        if (convertView == null) {
        	convertView = mInflater.inflate(R.layout.gallery_entry, null);
			holder = new ViewHolder(convertView);
			convertView.setTag(holder);
        }
        
        holder = (ViewHolder) convertView.getTag();
        webImageView = holder.getImageView();
        name = holder.getNameView();
        location = holder.getLocationView();
        // calling reset is important to prevent old images from displaying in a recycled view.
        webImageView.reset();

        webImageView.setImageUrl(food.imageUrl);
        webImageView.loadImage();
        
        name.setText(food.name);
        location.setText(food.locationName);
        location.setOnClickListener(new OnClickListener() {
			public void onClick(View v) {
				//start PlaceActivity
				if(food.id != 0) {
					location.setBackgroundResource(R.drawable.custom_link_bg_selector);
					Intent i = new Intent(mContext, PlaceActivity.class);
					i.putExtra(IntentExtras.PLACE_ID, food.locationId);
					mContext.startActivity(i);
				}
			}

		});
        
        onGetView(position, convertView, parent);

        return convertView;
    }

    protected void onGetView(int position, View convertView, ViewGroup parent) {
        // for extension
    }

    private class ViewHolder {
		private View vRow;
		private TextView vName = null;
		private TextView vLocation = null;
		public RemoteImageView vImage = null;

		public ViewHolder(View row) {
			vRow = row;
		}

		public TextView getNameView() {
			if (vName == null) {
				vName = (TextView) vRow.findViewById(R.id.name);
			}
			return vName;
		}
		
		public TextView getLocationView() {
			if (vLocation == null) {
				vLocation = (TextView) vRow.findViewById(R.id.location);
			}
			return vLocation;
		}

		public RemoteImageView getImageView() {
			if (vImage == null) {
				vImage = (RemoteImageView) vRow.findViewById(R.id.img);
			}
			return vImage;
		}
	}
}
